#!/bin/bash
. /etc/sipman.conf
# Inputsyntax
# $1 Section
# $2 ip addr device / all
# $3 snmp intno / all / type
# $4 type text/day/week/month/year
# -- Script begins here --
HTMLBASE=/var/www/html

testName ()
{
  # this procedure will only allow a-z 0-9 and - in names.
  #echo $1 | sed 's/_/-/g' | sed 's/ /_/g' | tr -cd '[:alnum:] [\-]' | tr '[A-Z]' '[a-z]'
  echo $1 | sed 's/_/-/g' | sed 's/ /_/g' | tr -cd '[:alnum:] [\-]'
}

rrdFetchDb ()
{
  local FILE=$1
  echo "<PRE>"
  $SIM_RRDTOOL fetch $FILE AVERAGE
  echo "<PRE>"
}
displayGraph ()
{
  local SYSNAME=$1
  local SECTION=$2
  local GRAPHTYPE=$3
  local DATADIR=$4
  local LFILENAME=$5
  local START=$6
  local GRAPHWIDTH=$7
  local PEAK=$8
  local REST="$9"
  TITLE=""
  DESCR=""
  SWITCH=no
  for VAR in $REST
  do
    if [ "$VAR" = "DESCR:" ]; then
      SWITCH=yes
    elif [ "$SWITCH" = "no" ]; then
      TITLE="$TITLE $VAR"
    else
      DESCR="$DESCR $VAR"
    fi
  done

#echo "SYSNAME=$SYSNAME SECTION=$SECTION GRAPHTYPE=$GRAPHTYPE DATADIR=$DATADIR LFILENAME=$LFILENAME START=$START GRAPHWIDTH=$GRAPHWIDTH PEAK=$PEAK DESCR=$DESCR"
# sleep 1
  case $START in
    (8hours)  TOFFSET=8h ;;
    (2days)   TOFFSET=2d ;;
    (week)    TOFFSET=1w ;;
    (2weeks)  TOFFSET=2w ;;
    (month)   TOFFSET=1m ;;
    (3months) TOFFSET=3m ;;
    (year)    TOFFSET=1y ;;
    (*)       TOFFSET=1d ;;
  esac
# COLORS=( 0 00FF00 FFFF00 2F4F4F 0000FF FF0000 FF00FF 00CED1 8FBC8F D2B48C )
# COLORS=( 0 FF0000 0000FF FFFF00 00FF00 FF1493 2F4F4F 8B0000 9400D3 00CED1 8FBC8F D2691E D2B48C )
  COLORS=( 0 0000FF FF0000 FFFF00 00FF00 FF1493 2F4F4F 8B0000 9400D3 00CED1 8FBC8F D2691E D2B48C )

  BASEURL=/graphs
  BOUTFILE=$LFILENAME-$START-$PEAK-$GRAPHWIDTH.png
  OUTFILE=$SIM_GRAPHTMPDIR/$BOUTFILE
  if [ ! -f $DATADIR/$LFILENAME.rrd ]; then
    echo "$DATADIR/$LFILENAME.rrd not found."
  fi
#           --units-exponent 0 \
# echo "$DATADIR/$LFILENAME $OUTFILE $TOFFSET<BR>"
  #SIGNATURE="Generated by Sipman at `date +%Y%m%d_%H%M%S`"
  SIGNATURE="$PEAK graph. Generated by Sipman at `date +%H.%M.%S` `date +%d-%m-%Y`"
  case $GRAPHTYPE in
    (intload-bits|intload-bytes)
      if [ "$GRAPHTYPE" = "intload-bits" ]; then
        FACTOR=1
      elif [ "$GRAPHTYPE" = "intload-bytes" ]; then
        FACTOR=8
      fi
#     echo "Factor:$FACTOR"
      INTNAME=`echo $DESCR | awk '{ print $1 }'`
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="$TITLE" \
            --vertical-label "bits p/s" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            CDEF:cdefa=linea,$FACTOR,* \
            CDEF:cdefb=lineb,$FACTOR,* \
            AREA:cdefa#00FF00:"In traffic bps" \
            LINE1:cdefb#0000FF:"Out traffic bps" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
#           GPRINT:inbits:MAX:%lf%s \
#           GPRINT:outbits:MAX:%lf%s \
    ;;
    (int-HCbroadcasts)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="$TITLE" \
            --vertical-label "packets p/s" \
            --upper-limit 50 \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            DEF:linec=$DATADIR/$LFILENAME.rrd:var3:$PEAK \
            DEF:lined=$DATADIR/$LFILENAME.rrd:var4:$PEAK \
            LINE1:linea#0000FF:"In broadcasts" \
            LINE1:lineb#FF0000:"In multicasts" \
            LINE1:linec#FF00FF:"Out broadcasts" \
            LINE1:lined#00FF00:"Out multicasts" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (int-broadcasts)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="$TITLE" \
            --vertical-label "broadcasts p/s" \
            --upper-limit 50 \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            AREA:linea#00FF00:"In broadcasts" \
            LINE1:lineb#0000FF:"Out broadcasts" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (ciscoVPNcpu)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Cpu load on $SYSNAME." \
            --vertical-label "cpuload" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            AREA:linea#00FF00:"Cpu load" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (ciscoVPNActiveSessions)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Sessions on $SYSNAME." \
            --vertical-label "Active sessions" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            AREA:linea#00FF00:"Active sessions" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (ciscocpu1)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Cpu load on $SYSNAME." \
            --vertical-label "cpuload" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            DEF:linec=$DATADIR/$LFILENAME.rrd:var3:$PEAK \
            AREA:linea#00FF00:"Cpu 5 sec" \
            LINE1:lineb#0000FF:"Cpu 1 min" \
            LINE1:linec#FF00FF:"Cpu 5 min" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (ciscomemavail)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Available memory on $SYSNAME." \
            --vertical-label "memory" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            AREA:linea#00FF00:"Available memory" \
            LINE1:lineb#0000FF:"Largest memory block" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (ciscobuff|dnsstats|dhcpstats|int-errors|int-brocadeerrors|int-ciscoerrors|int-rmonerrors|rtt-mon)
        # Statistics for lines / min
        if [ "$GRAPHTYPE" = "ciscobuff" ]; then
          VARNAMES="SmBuff MdBuff LgBuff ElBuff BgBuff"
          VERTLABEL="buff p/min"
          UPPERLIMIT=1
	  FACTOR=60
        elif [ "$GRAPHTYPE" = "dnsstats" ]; then
	  VARNAMES="A PTR ANY MX NS CNAME SRV SOA AAAA"
	  VERTLABEL="queries p/min"
	  UPPERLIMIT=1
	  FACTOR=60
	elif [ "$GRAPHTYPE" = "dhcpstats" ]; then
          VARNAMES="DHCPDISCOVER DHCPOFFER DHCPREQUEST DHCPACK DHCPINFORM DHCPRELEASE DHCPNAK"
	  VERTLABEL="actions p/min"
	  UPPERLIMIT=1
	  FACTOR=60
	elif [ "$GRAPHTYPE" = "int-errors" ]; then
          VARNAMES="InErrors OutErrors"
          VERTLABEL="errors / min"
          UPPERLIMIT=10
	  FACTOR=60
	elif [ "$GRAPHTYPE" = "int-ciscoerrors" ]; then
          VARNAMES="InRunts InGiants InCRC InFrame InOverrun InIgnored InAbort Resets CarTrans Collisions InQueDrops OutQueDrops"
          VERTLABEL="errors / min"
	  UPPERLIMIT=10
	  FACTOR=60
	elif [ "$GRAPHTYPE" = "int-brocadeerrors" ]; then
          VARNAMES="RxEncInFrs RxCrcs RxTruncs RxTooLongs RxBadEofs RxEncOutFrs RxBadOs C3Discards McastTimedOuts"
          VERTLABEL="errors / min"
          UPPERLIMIT=10
          FACTOR=60
	elif [ "$GRAPHTYPE" = "int-rmonerrors" ]; then
	  VARNAMES="DropEvents CRCAlignErrors UndersizePkts OversizePkts Fragments Jabbers Collisions"
	  VERTLABEL="errors / min"
	  UPPERLIMIT=10
	  FACTOR=60
	elif [ "$GRAPHTYPE" = "rtt-mon" ]; then
	  VARNAMES="ResponseTime"
	  VERTLABEL="rtt m/sec"
	  UPPERLIMIT=10
	  FACTOR=1
	fi
	    RRDPREFIX="--start -$TOFFSET \
	            --imgformat PNG \
		    --width $GRAPHWIDTH \
		    --upper-limit $UPPERLIMIT \
		    "

       COUNT=1
       GRAPHDEF=""
       GRAPHCDEF=""
       GRAPHLINE=""
       for VARVAL in $VARNAMES
       do
         IFILENAME=$SYSNAME-$SECTION-$GRAPHTYPE-$VARVAL-$START-$PEAK-$GRAPHWIDTH
         GRAPHLDEF="DEF:linevar$COUNT=$DATADIR/$LFILENAME.rrd:var$COUNT:$PEAK"
         GRAPHDEF="$GRAPHDEF $GRAPHLDEF"
	 GRAPHLCDEF="CDEF:linecvar$COUNT=linevar$COUNT,$FACTOR,*"
	 GRAPHCDEF="$GRAPHCDEF $GRAPHLCDEF"
	 GRAPHLLINE="LINE2:linecvar$COUNT#${COLORS[$COUNT]}:$VARVAL"
	 GRAPHLINE="$GRAPHLINE $GRAPHLLINE"
	 COUNT=`expr $COUNT + 1`
	 if [ "$ACTION" = "detail" ]; then
	   $SIM_RRDTOOL graph $SIM_GRAPHTMPDIR/$IFILENAME.png \
	         --vertical-label "$VERTLABEL" \
		 --title="$TITLE" \
	         $RRDPREFIX \
		 $GRAPHLDEF \
		 $GRAPHLCDEF \
		 $GRAPHLLINE \
		 COMMENT:"$DESCR" \
		 COMMENT:"$SIGNATURE" > /dev/null
           #echo "<A HREF=$SCRIPT_NAME?graph+timescale+$SECTION+$SELECTDEVICE+$LFILENAME
           echo "<A HREF=$SCRIPT_NAME?graph+timescale+$SECTION+$SYSNAME+$LFILENAME+$PEAK
	             <IMG SRC=$BASEURL/$IFILENAME.png>
		               </A>"
	 fi
       done
       if [ "$ACTION" != "detail" ]; then
         $SIM_RRDTOOL graph $OUTFILE \
           --vertical-label "$VERTLABEL" \
           --title="$TITLE" \
           $RRDPREFIX \
           $GRAPHDEF \
           $GRAPHCDEF \
           $GRAPHLINE \
           COMMENT:"$DESCR" \
           COMMENT:"$SIGNATURE" > /dev/null
         SKIP=no
       fi
     ;;


    (linuxcpu1)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Cpu load on $SYSNAME." \
            --vertical-label "cpuload" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            AREA:linea#00FF00:"User load" \
            STACK:lineb#FF0000:"System load" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (linux-sysload)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Systemload statistics on $SYSNAME." \
            --vertical-label "count" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            DEF:linec=$DATADIR/$LFILENAME.rrd:var3:$PEAK \
            DEF:lined=$DATADIR/$LFILENAME.rrd:var4:$PEAK \
            DEF:linee=$DATADIR/$LFILENAME.rrd:var5:$PEAK \
            DEF:linef=$DATADIR/$LFILENAME.rrd:var6:$PEAK \
            LINE2:linea#00FF00:"SwapIn" \
            LINE2:lineb#FFFF00:"SwapOut" \
            LINE2:linec#00FFFF:"IOSent" \
            LINE2:lined#0000FF:"IOReceive" \
            LINE2:linee#FF0000:"SysInterrupts" \
            LINE2:linef#FF00FF:"SysContext" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;

    (linuxmem)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Used memory on $SYSNAME." \
            --vertical-label "Memory" \
            --width $GRAPHWIDTH \
            DEF:dat1=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:dat2=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            CDEF:linea=dat1,1024,* \
            CDEF:lineb=dat2,1024,* \
            AREA:linea#00FF00:"Real mem" \
            LINE1:lineb#0000FF:"Swap mem" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (linuxusrproc)
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Users and processes on $SYSNAME." \
            --vertical-label "count" \
            --width $GRAPHWIDTH \
            DEF:linea=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            DEF:lineb=$DATADIR/$LFILENAME.rrd:var2:$PEAK \
            AREA:linea#00FF00:"Users" \
            LINE1:lineb#0000FF:"Processes" \
            COMMENT:"$DESCR" \
            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (linuxvolume)
      PARAM=( `echo $DESCR` )
      $SIM_RRDTOOL graph $OUTFILE --start -$TOFFSET \
            --imgformat PNG \
            --title="Volume info for ${PARAM[2]} on $SYSNAME (${PARAM[3]} Mb)." \
            --vertical-label "available diskspace" \
            --width $GRAPHWIDTH \
            DEF:used=$DATADIR/$LFILENAME.rrd:var1:$PEAK \
            CDEF:avail=${PARAM[0]},used,-,${PARAM[1]},* \
            AREA:avail#00FF00:"Diskspace" \
            COMMENT:"Volume ${PARAM[2]} (${PARAM[3]} Mb)" \
            COMMENT:"$SIGNATURE" > /dev/null
#     echo "$DESCR"
#            CDEF:avail=${PARAM[0]},used,-,${PARAM[1]},* \
#            AREA:avail#00FF00:"Diskspace" \
#            COMMENT:"$SIGNATURE" > /dev/null
        SKIP=no
    ;;
    (*) SKIP=yes ; echo "$GRAPHTYPE $DESCR<BR>" ;;
  esac
#     <A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SELECTDEVICE+8hours+$WIDTH>8 hours</A> -
  if [ "$SKIP" = "no" ]; then
    if [ "$ACTION" = "main" ] || [ "$ACTION" = "thresholds" ]; then
      NEWACTION=timescale
    elif [ "$ACTION" = "interface-graphs" ]; then
      NEWACTION=timescale
    else
      NEWACTION=detail
    fi
    echo "<A HREF=$SCRIPT_NAME?graph+$NEWACTION+$SECTION+$SELECTDEVICE+$LFILENAME+$PEAK+$START>
            <IMG SRC=$BASEURL/$BOUTFILE>
          </A>"
  fi


}

dispThresholds ()
{
  local SECTION=$1
  local HIST=$2
  local WORKDIR="$SIM_GRAPHTHRESD/$SECTION/$HIST"
  echo "<H3>Displaying thresholds for $SECTION.</H3>"
  ls $WORKDIR | while read TFILE
  do
    INFO=`cat $WORKDIR/$TFILE 2>/dev/null 2>/dev/null`
    DEVICE=`echo $INFO | awk '{ print $1 }'`
    DIR=`echo $INFO | awk '{ print $2 }'`
    FILE=`echo $INFO | awk '{ print $3 }'`
    #echo "<BR>TFILE=$TFILE, DEVICE=$DEVICE, DIR=$DIR, FILE=$FILE.<BR>"
    if [ "$FILE" != "$OLDFILE" ]; then
      grep " $FILE " $SIM_GRAPHCONFDIR/$SECTION/$DEVICE/graphinfo.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
      do
        #echo "displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680, $REST"
        #displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 "$REST"
	SELECTDEVICE=$SYSNAME
        displayGraph $SYSNAME $SECTION $GRAPHTYPE $DATADIR $FILENAME week 680 $PEAK "$REST"
      done
    fi
    OLDFILE=$FILE
  done
}

#------------------------------ Main ---------------------------------
if [ "$1" = "" ] || [ "$1" = "none" ]; then
  ACTION=main
else
  ACTION=$1
fi
if [ "$2" = "" ]; then
  SECTION=none
else
  SECTION=$2
fi
if [ "$3" = "" ]; then
  SELECTDEVICE="none"
elif [ "$3" = "form"  ]; then
  SELECTDEVICE=$FORM_selectDevice
else
  SELECTDEVICE=$3
fi
if [ "$4" = "" ]; then
  SELECTGRAPH="any"
else
  SELECTGRAPH=$4
fi
if [ "$5" = "MAX" ] || [ "$5" = "AVERAGE" ]; then
  PEAK=$5
else
  PEAK="AVERAGE"
fi
if [ "$6" = "" ] || [ "$6" = "AVERAGE" ]; then
  TIME="day"
else
  TIME=$6
fi
if [ "$7" = "" ]; then
  WIDTH="330"
else
  WIDTH=$7
fi

#echo "ACTION=$ACTION, SECTION=$SECTION, SELECTDEVICE=$SELECTDEVICE, SELECTGRAPH=$SELECTGRAPH, PEAK=$PEAK, TIME=$TIME WIDTH=$WIDTH<HR>"


# aanvullende opbouw kop pagina afhankelijk van de query.
echo "<B>Graph type</B>:
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+AVERAGE+$TIME+$WIDTH>AVERAGE</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+MAX+$TIME+$WIDTH>MAX</A> -
      Time:
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+8hours+$WIDTH>8 hours</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+day+$WIDTH>day</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+2days+$WIDTH>2 days</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+week+$WIDTH>week</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+2weeks+$WIDTH>2 weeks</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+month+$WIDTH>Month</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+year+$WIDTH>Year</A> 
      Width:
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME+225>225</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME+330>330</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME+450>450</A> -
      <A HREF=$SCRIPT_NAME?graph+$ACTION+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME+640>640</A> 
      /
      <A HREF=$SCRIPT_NAME?graph+thresholds>Thresholds</A> |
      <A HREF=$SCRIPT_NAME?graph+remSpike+$SECTION+$SELECTDEVICE>Remove spikes</A> |
      <A HREF=$SCRIPT_NAME?graph+selectReport+$SECTION+$SELECTDEVICE>Report</A>
     "
echo "</TD></TR>"
echo "<TR><TD COLSPAN=3><CENTER>Sections:"

ls $SIM_GRAPHCONFDIR | while read TYPE
do
  if [ -d $SIM_GRAPHCONFDIR/$TYPE ]; then
    if [ "$ACTION" = "thresholds" ]; then
      DISP_ACTION=thresholds
    else
      DISP_ACTION=main
    fi
#   echo " .<A HREF=$SCRIPT_NAME?graph+main+$TYPE+none+$SELECTGRAPH+$PEAK+$TIME+$WIDTH>$TYPE</A>. "
#   echo " .<A HREF=$SCRIPT_NAME?graph+main+$TYPE+none+$SELECTGRAPH+$PEAK+$TIME+$WIDTH>$TYPE</A>. "
    echo " .<A HREF=$SCRIPT_NAME?graph+$DISP_ACTION+$TYPE+none+$SELECTGRAPH+$PEAK+$TIME+$WIDTH>$TYPE</A>. "
  fi
done
CACHEFILE=$SIM_TMPDIR/graphs-$SECTION-$REMOTE_USER
if [ "$ACTION" = "main" ] && [ "$SECTION" != "none" ]; then
  echo "</TD></TR><TR><TD COLSPAN=3><CENTER>"
  if [ "$SELECTDEVICE" = "none" ]; then
    echo "Devices: "
    echo "  " > $CACHEFILE
    # koptext met devices toevoegen.
    OLDSYSNAME=""
#   echo "<PRE>. cat $SIM_GRAPHCONFDIR/$SECTION/graphinfo-*.conf .</PRE>" 
#   cd $SIM_GRAPHCONFDIR/$SECTION
#   ls | while read FILE
#   do
#     echo "graph ${FILE:0:10} ${FILE:10} <BR>"
#   done
    #ls $SIM_GRAPHCONFDIR/$SECTION | grep graphinfo- | while read FILE
    ls $SIM_GRAPHCONFDIR/$SECTION | while read SYSNAME
    do
      if [ -s $SIM_GRAPHCONFDIR/$SECTION/$SYSNAME/graphinfo.conf ]; then
        echo " .<A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SYSNAME+$SELECTGRAPH+$PEAK+$TIME+$WIDTH>$SYSNAME</A>. "
	echo "<OPTION VALUE=$SYSNAME>$SYSNAME</OPTION>" >> $CACHEFILE
      elif [ -d $SIM_GRAPHCONFDIR/$SECTION/$SYSNAME ]; then
        echo " <FONT COLOR=red>D: $SYSNAME </FONT>"
        rm -fR $SIM_GRAPHCONFDIR/$SECTION/$SYSNAME
      elif [ -f $SIM_GRAPHCONFDIR/$SECTION/$SYSNAME ]; then
        echo " <FONT COLOR=red>F: $SYSNAME </FONT>"
        rm $SIM_GRAPHCONFDIR/$SECTION/$SYSNAME
      fi
    done
# This procedure deliveres problems on AIX around 250 systems
#   cat $SIM_GRAPHCONFDIR/$SECTION/graphinfo-*.conf 2>/dev/null| while read SYSNAME REST
#   do
#     if [ "$OLDSYSNAME" != "$SYSNAME" ]; then
#       echo ".<A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SYSNAME+$SELECTGRAPH+$PEAK+$TIME+$WIDTH>$SYSNAME</A>."
#       echo "<OPTION VALUE=$SYSNAME>$SYSNAME</OPTION>" >> $CACHEFILE
#       OLDSYSNAME=$SYSNAME
#     fi
#   done

  else
    #echo "$SELECTDEVICE"
    echo "<FORM method=post Action=$SCRIPT_NAME?graph+main+$SECTION+form+any+$PEAK+$TIME+$WIDTH>
          <SELECT NAME=selectDevice> <OPTION VALUE=$SELECTDEVICE>$SELECTDEVICE</OPTION> "
    cat $CACHEFILE 2>/dev/null
    echo "</SELECT> <INPUT TYPE=submit Value=Select> </FORM>"
  fi
elif [ "$ACTION" = "thresholds" ] && [ "$SECTION" != "none" ]; then
  dispThresholds $SECTION current
fi
echo "</TD></TR></TABLE><CENTER>"
if [ "$SELECTDEVICE" != "none" ]; then
  if [ "$SELECTGRAPH" = "text" ]; then
    echo "<H1>Overview volumes on device $SELECTDEVICE for the timespan of a $TIME</H1><HR>"
  elif [ "$ACTION" = "interface-graphs" ]; then
    #graph+main+cisco+mah06c3750-01+any+day+225
    echo "<H1>Interface graphs on <A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SELECTDEVICE+any+day+225>$SELECTDEVICE</A></H1>"
  elif [ "$ACTION" = "report" ]; then
    echo "<H2>Add the graph to a report file.</H2>
        <TABLE>
        <TR><TD>Section</TD><TD>$SECTION</TD></TR>
        <TR><TD>Device</TD><TD>$SELECTDEVICE</TD></TR>
        <TR><TD>Graphname</TD><TD>$SELECTGRAPH</TD></TR>
        </TABLE>
        "
  elif [ "$ACTION" = "selectReport" ]; then
    echo "<H1>Select an report below.</H1>"
  elif [ "$ACTION" = "runReport" ]; then
    echo "<H1>Report $SECTION.</H1>"
  else
    echo "<H1>Graphs for device $SELECTDEVICE for the timespan of a $TIME</H1>"
  fi
fi

if [ "$ACTION" = "remSpike" ]; then
  echo "<H3>removing spikes for $SELECTDEVICE.</H3>"
  BASEDIR="$SIM_GRAPHDATADIR/$SECTION/$SELECTDEVICE"
  echo "$BASEDIR"
  echo "</CENTER><PRE>"
  ls $BASEDIR | while read FILE
  do
    echo "<B>$FILE</B>"
    $SIM_REMSPIKET $BASEDIR/$FILE | sed 's/\>/\&gt;/g' | sed 's/\</\&lt;/g' 2>&1
    if [ -f "$BASEDIR/$FILE.old" ]; then
      rm $BASEDIR/$FILE.old
    fi
  done
  POLLFILE="$SIM_GRAPHCONFDIR/$SECTION/pollinfo-$SELECTDEVICE.conf"
  if [ -f "$POLLFILE" ]; then
    DEVICE_IP=`grep SNMP $POLLFILE | awk '{ print $2 }'`
    echo "<BR><B>Regenerating graph config for $DEVICE_IP</B><HR>"
    $SIM_SUDO $SIM_CONFRRDTOOL confnode $DEVICE_IP 2>&1
  fi
  echo "</PRE><CENTER>"
elif [ "$ACTION" = "main" ]; then
  echo "<TABLE><TR><TD VALIGN=TOP>"
  OLDCOLNUM="1"
  FIRST="YES"
  cat $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf 2>/dev/null| while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST 
  do
    if [ "$SELECTDEVICE" = "none" ]; then
      echo "<H3>Please select a device or all from the list above....</H3>"
      break
    elif [ "$SELECTDEVICE" = "$SYSNAME" ] || [ "$SELECTDEVICE" = "all" ]; then
      # no lcase needed.
      LFILENAME=$FILENAME
      if [ "$OLDSYSNAME" != "$SYSNAME" ]; then
          NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
          if [ -f $NETDIR/snmpoid.txt ]; then
            OIDNAME=`cat $NETDIR/snmpoid.txt 2>/dev/null`
          else
            OIDNAME=""
          fi
          echo "</TD></TR></TABLE><HR><H3>$SYSNAME
                <FONT SIZE=-1><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR>$OIDNAME</A></FONT>
                </H3><TABLE><TR><TD VALIGN=TOP>"
        OLDSYSNAME=$SYSNAME
      fi
      if [ "$GRAPHTYPE" = "RULE:" ]; then
        echo "</TD></TR><TR><TD BGCOLOR=#DDDDDD COLSPAN=4><FONT=+1>$DATATYPE $DATADIR $COLNUM $FILENAME $REST"
        OLDCOLNUM=9
      elif [ "$GRAPHTYPE" = "DESCR:" ]; then
        for VAR in $DATATYPE $DATADIR $COLNUM $FILENAME $REST
	do
          GRAPHDESCR="$GRAPHDESCR $VAR"
	done
      else
        if [ "$COLNUM" -gt "$OLDCOLNUM" ]; then
          echo "</TD><TD VALIGN=TOP>"
        elif [ "$COLNUM" -lt "$OLDCOLNUM" ]; then
          echo "</TD></TR><TR><TD VALIGN=TOP>"
        fi
        OLDCOLNUM=$COLNUM
#       echo "$GRAPHTYPE $LFILENAME<BR>"
        displayGraph $SYSNAME $SECTION $GRAPHTYPE $DATADIR $LFILENAME $TIME $WIDTH $PEAK "$REST" 
      fi
    fi
  done
elif [ "$ACTION" = "detail" ]; then
  echo "<HR>
        <A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SELECTDEVICE+any+$PEAK+day+225>Device</A> / 
        <A HREF=$SCRIPT_NAME?graph+database+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK>Database</A> /
        <A HREF=$SCRIPT_NAME?graph+report+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME>Report</A>
                                       "
  echo "<H3>Individual graphs for $SELECTDEVICE $SELECTGRAPH.</H3>"
  grep " $SELECTGRAPH " $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
  do
#   echo "displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 $REST"
    displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 $PEAK "$REST"
  done
elif [ "$ACTION" = "report" ]; then
  echo "<HR> $WIDTH"
  TESTNAME=$WIDTH
  if [ "$TESTNAME" = "330" ]; then
    TESTNAME="$FORM_Report"
  fi
  TESTNAME=`testName "$TESTNAME"`
  if [ "$TESTNAME" = "" ]; then
    echo "<TABLE>
      <TR><TD>
      <FORM method=post Action=$SCRIPT_NAME?graph+report+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME>
      <INPUT NAME=Report Size=80 TYPE=text>
      <INPUT TYPE=submit Value=Select>
      </FORM>
      </TD></TR>
         "
    cd $SIM_GRAPHREPORTDIR
    ls | while read NAME
    do
      echo "<TR><TD><A HREF=$SCRIPT_NAME?graph+report+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME+$NAME>$NAME</A></TD></TR>"
    done
    echo "</TABLE>"
  else
    echo "Adding definitions to report file \"$TESTNAME\"."
    echo "graph $SECTION $SELECTDEVICE $SELECTGRAPH $PEAK $TIME" >> $SIM_GRAPHREPORTDIR/$TESTNAME
  fi
elif [ "$ACTION" = "selectReport" ]; then
  echo "<HR><TABLE>"
  cd $SIM_GRAPHREPORTDIR
    ls | while read NAME
    do
      echo "<TR><TD><A HREF=$SCRIPT_NAME?graph+runReport+$NAME+report+none+none+$PEAK+$WIDTH>$NAME</A></TD></TR>"
    done
    echo "</TABLE>"
elif [ "$ACTION" = "runReport" ]; then
  echo "<HR>"
  cat $SIM_GRAPHREPORTDIR/$SECTION | while read TYPE SECTION SELECTDEVICE GRAPH PEAK TIME
  do
    #echo "$TYPE, $SECTION, $SELECTDEVICE, $GRAPH, $PEAK.<BR>"
    if [ "$TYPE" = "H1" ]; then
      echo "<H1>$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME</H1>"
    elif [ "$TYPE" = "H2" ]; then
      echo "<H2>$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME</H2>"
    elif [ "$TYPE" = "H5" ]; then
      echo "<P><B>$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME</B></BR>"
    elif [ "$TYPE" = "BR" ]; then
      echo "<BR>$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME<BR>"
    elif [ "$TYPE" = "P" ]; then
      echo "<P>$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME<BR>"
    elif [ "$TYPE" = "txt" ]; then
      echo "$SECTION $SELECTDEVICE $GRAPH $PEAK $TIME<BR>"
    elif [ "$TYPE" = "graph" ]; then
      grep " $GRAPH " $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
      do
        #echo "displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 $PEAK \"$REST\"<BR>"
        displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 580 $PEAK "$REST"
        echo "<BR>"
      done
    fi
  done
elif [ "$ACTION" = "interface-graphs" ]; then
  echo "<HR>"
  for ENTRY in "$SELECTGRAPH\\." "$SELECTGRAPH\\-"
  do
#   echo "grep $ENTRY $SIM_GRAPHCONFDIR/$SECTION/graphinfo-$SELECTDEVICE.conf<HR>"
    grep  $ENTRY  $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
    do
      displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 $PEAK "$REST"
    done
  done
elif [ "$ACTION" = "timescale" ]; then
  echo "<HR> TIME=$TIME
        <A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SELECTDEVICE+any+$PEAK+day+225>Device</A> / 
        <A HREF=$SCRIPT_NAME?graph+database+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK>Database</A> /
        <A HREF=$SCRIPT_NAME?graph+report+$SECTION+$SELECTDEVICE+$SELECTGRAPH+$PEAK+$TIME>Report</A>
       "
  echo "<H3>Detail info for $SELECTDEVICE $SELECTGRAPH.</H3>"
# grep " $SELECTGRAPH " $SIM_GRAPHCONFDIR/$SECTION/graphinfo-*.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
  grep " $SELECTGRAPH " $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf | while read SYSNAME IPADDR GRAPHTYPE DATATYPE DATADIR COLNUM FILENAME REST
  do
    for TIME in day week month year
    do
      case $TIME in
        (day)   echo "<H4>Day graph, 5 min $PEAK($REST).</H4>" ;;
        (week)  echo "<H4>Week graph, 30 min $PEAK.</H4>" ;;
        (month) echo "<H4>Month graph, 2 hour $PEAK.</H4>" ;;
        (year)  echo "<H4>Year graph, day $PEAK.</H4>" ;;
      esac
      displayGraph $SELECTDEVICE $SECTION $GRAPHTYPE $DATADIR $FILENAME $TIME 680 $PEAK "$REST"
    done
  done
elif [ "$ACTION" = "database" ]; then
  echo "<H3>Database info for $SELECTDEVICE $SELECTGRAPH.</H3>"
  RECCORD=(`grep " $SELECTGRAPH " $SIM_GRAPHCONFDIR/$SECTION/$SELECTDEVICE/graphinfo.conf` )
  DATADIR="${RECCORD[4]}"
  LFILENAME=${RECCORD[6]}
  echo "<A HREF=$SCRIPT_NAME?graph+removeDbFile+$SECTION+$SELECTDEVICE+$SELECTGRAPH>remove Database file</A>
        <PRE>"
  echo "<H2>Database info.</H2>"
# $SIM_RRDTOOL fetch $SIM_GRAPHDATADIR/$DATADIR/$LFILENAME.rrd AVERAGE 2>&1
  $SIM_RRDTOOL info $DATADIR/$LFILENAME.rrd 2>&1
  echo "<H2>Database polling data for 24 hours.</H2>"
  $SIM_RRDTOOL fetch $DATADIR/$LFILENAME.rrd AVERAGE 2>&1
  echo "</PRE>"
elif [ "$ACTION" = "removeDbFile" ]; then
  echo "<A HREF=$SCRIPT_NAME?graph+main+$SECTION+$SELECTDEVICE+any+$PEAK+day+225>Device</A><HR>"
  DBFILE="$SIM_GRAPHDATADIR/$SECTION/$SELECTDEVICE/$SELECTGRAPH.rrd"
  if [ -f "$DBFILE" ]; then
    echo "Removing DbFile $SELECTGRAPH.rrd."
    rm $DBFILE
  else
    echo "Oops, could not find file $DBFILE."
  fi
fi
echo "</TD></TR></TABLE>"
